﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wicresoft.EKA.CIImportApp.Init;
using NRemedy;

namespace Wicresoft.EKA.CIImportApp
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine(" To import CI Data To ITSM!...\n Please click Enter key!");
            Console.ReadLine();
            Console.WriteLine(" Starting...\n");

            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:\\Wicresoft.EKA.CIImportApp\\CI_Data\\CI_Import_Data.xls;Extended Properties=Excel 8.0 ";

            OleDbConnection myConn = new OleDbConnection(strCon);
            myConn.Open();

            //string[] Sheet = new string[] { "Hardware","AppSystem","SysSoftware" };
            string[] Sheet = new string[] { "Hardware" };
            for (int n = 0; n < Sheet.Length; n++)
            {
                DataSet myDataSet = new DataSet();
                string StrSql = "select * from [" + Sheet[n] + "$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
                myCommand.Fill(myDataSet, "[" + Sheet[n] + "$]");
                myCommand.Dispose();
                DataTable DT = myDataSet.Tables["[" + Sheet[n] + "$]"];
                myConn.Close();
                myCommand.Dispose();

                if (Sheet[n].ToString() == "Hardware")
                {
                    ASTBMC_Hardware model = new ASTBMC_Hardware();
                    for (int j = 2; j < DT.Rows.Count; j++)
                    {
                        model.Category = DT.Rows[j][0].ToString();
                        model.Type = DT.Rows[j][1].ToString();
                        model.Item = DT.Rows[j][2].ToString();
                        model.DeviceName = DT.Rows[j][3].ToString();
                        model.VendorApplication = DT.Rows[j][4].ToString();
                        model.SerialNumber = DT.Rows[j][5].ToString();
                        //model.Name = DT.Rows[j][6].ToString();
                        model.Model = DT.Rows[j][7].ToString();
                        model.ManufacturerName = DT.Rows[j][8].ToString();
                        model.SupplierName = DT.Rows[j][9].ToString();
                        model.Room = DT.Rows[j][10].ToString();
                        model.IPAddress = DT.Rows[j][11].ToString();
                        model.Usaged = DT.Rows[j][12].ToString();
                        model.BuyTime = Convert.ToDateTime(DT.Rows[j][13]);
                        model.Principal = DT.Rows[j][14].ToString();
                        model.PrincipalDept = DT.Rows[j][15].ToString();
                        model.SystemAndVersion = DT.Rows[j][16].ToString();
                        model.ExpireTime = Convert.ToDateTime(DT.Rows[j][17]);
                        model.CpuCore = Convert.ToDouble(DT.Rows[j][18]);
                        model.MemoryCapacity = Convert.ToDouble(DT.Rows[j][19]);
                        model.StorageCapacity = Convert.ToDouble(DT.Rows[j][20]);
                        model.PhysicalPath = DT.Rows[j][21].ToString();
                        model.HostName = DT.Rows[j][22].ToString();
                        model.HotBackupCore = Convert.ToDouble(DT.Rows[j][23]);
                        model.ControllerCore = Convert.ToDouble(DT.Rows[j][24]);
                        model.CacheCapacity = Convert.ToDouble(DT.Rows[j][25]);
                        model.DeviceVersion = DT.Rows[j][26].ToString();
                        model.RAIDLevel = DT.Rows[j][27].ToString();
                        model.OtherName1 = DT.Rows[j][28].ToString();
                        model.OtherName2 = DT.Rows[j][29].ToString();
                        model.IsVirtualMachine = DT.Rows[j][30].ToString();
                        model.Company = DT.Rows[j][31].ToString();

                        model.MarkAsDeleted = 0;
                        model.DatasetId = "BMC.ASSET";
                        model.Submitter = "Demo";
                        model.IP = model.IPAddress;
                        //model.Create_Date = DateTime.Now;
                        //model.Last_Modifed_By = "Demo";
                        //model.Modifed_Date = DateTime.Now;
                        model.SubmitFullName = "Demo";
                        model.ModifyFullName = "Demo";
                        model.Short_Description = "BMC_Hardware";
                        model.zSchemaProperName = "BMC_Hardware";

                        bool ret = DataInit.ImportHardware(model);
                        string msg = "Import "+ DT.Rows[j][0].ToString() +" DeviceName = " + model.DeviceName + " ;" + ret.ToString();
                        if (ret)
                            SysLog.WriteSysLog(msg);
                        Console.WriteLine(msg);

                    }
                }
                else if (Sheet[n].ToString() == "AppSystem")
                {
                    ASTBMC_AppSystem model = new ASTBMC_AppSystem();
                    for (int j = 2; j < DT.Rows.Count; j++)
                    {
                        model.Category = DT.Rows[j][0].ToString();
                        model.Type = DT.Rows[j][1].ToString();
                        model.Item = DT.Rows[j][2].ToString();
                        model.AppSystemName = DT.Rows[j][3].ToString();
                        model.Abbreviation = DT.Rows[j][4].ToString();
                        model.AppSysVersionNumber = DT.Rows[j][5].ToString();
                        //model.Name = DT.Rows[j][6].ToString();
                        model.ManufacturerName = DT.Rows[j][7].ToString();
                        model.IPAddress = DT.Rows[j][8].ToString();
                        model.AppSite = DT.Rows[j][9].ToString();
                        model.SysBusinessAdminDept = DT.Rows[j][10].ToString();
                        model.SysResponsibleDept = DT.Rows[j][11].ToString();
                        model.SecurityLevel = DT.Rows[j][12].ToString();
                        model.SubSysName = DT.Rows[j][13].ToString();
                        model.BusinessType = DT.Rows[j][14].ToString();
                        model.Function = DT.Rows[j][15].ToString();
                        model.OperationAccountNumber = DT.Rows[j][16].ToString();
                        model.NetworkModel = DT.Rows[j][17].ToString();
                        model.Owner = DT.Rows[j][18].ToString();
                        model.AuthenticationStyle = DT.Rows[j][19].ToString();
                        model.OperationAccount = DT.Rows[j][20].ToString();
                        model.AllowWebSite = DT.Rows[j][21].ToString();
                        model.UserNumber = DT.Rows[j][22].ToString();
                        model.DataType = DT.Rows[j][23].ToString();
                        model.DataSubType = DT.Rows[j][24].ToString();
                        model.ApplicationRushHours = DT.Rows[j][25].ToString();
                        model.BusinessName = DT.Rows[j][26].ToString();
                        model.UserRange = DT.Rows[j][27].ToString();
                        model.UserRole = DT.Rows[j][28].ToString();
                        model.DataDayIncrement = DT.Rows[j][29].ToString();
                        model.DataSum = DT.Rows[j][30].ToString();
                        model.StorageLife = DT.Rows[j][31].ToString();
                        model.BackupCycle = DT.Rows[j][32].ToString();
                        model.BackupStyle = DT.Rows[j][33].ToString();
                        model.DigitalSignatures = DT.Rows[j][34].ToString();
                        model.SystemAndVersion = DT.Rows[j][35].ToString();
                        model.DBNameAndVersion = DT.Rows[j][36].ToString();
                        model.MiddlewareNameAndVersion = DT.Rows[j][37].ToString();
                        model.SaveLocal = DT.Rows[j][38].ToString();
                        model.ExpireTime = Convert.ToDateTime(DT.Rows[j][39]);
                        model.GuaranteeClause = DT.Rows[j][40].ToString();
                        model.FirstOnlineTime = Convert.ToDateTime(DT.Rows[j][41]);
                        model.Platform = DT.Rows[j][42].ToString();
                        model.Company = DT.Rows[j][43].ToString();

                        model.MarkAsDeleted = 0;
                        model.DatasetId = "BMC.ASSET";
                        model.Submitter = "Demo";
                        //model.Create_Date = DateTime.Now;
                        //model.Last_Modifed_By = "Demo";
                        //model.Modifed_Date = DateTime.Now;
                        model.SubmitFullName = "Demo";
                        model.ModifyFullName = "Demo";
                        model.Short_Description = "BMC_AppSystem";
                        model.zSchemaProperName = "BMC_AppSystem";
                        model.IP=model.IPAddress;
                        model.SaveOffsite = "福建烟草";

                        bool ret = DataInit.ImportAppSystem(model);
                        string msg = "Import " + DT.Rows[j][0].ToString() + " DeviceName = " + model.AppSysName + " ;" + ret.ToString();
                        if(ret)
                            SysLog.WriteSysLog(msg);
                        Console.WriteLine(msg);

                    }
                }
                else if (Sheet[n].ToString() == "SysSoftware")
                {
                    ASTBMC_SysSoftware model = new ASTBMC_SysSoftware();
                    for (int j = 2; j < DT.Rows.Count; j++)
                    {
                        model.Category = DT.Rows[j][0].ToString();
                        model.Type = DT.Rows[j][1].ToString();
                        model.Item = DT.Rows[j][2].ToString();
                        model.DeviceName = DT.Rows[j][3].ToString();
                        //model.Name = DT.Rows[j][4].ToString();
                        model.ManufacturerName = DT.Rows[j][5].ToString();
                        model.IPAddress = DT.Rows[j][6].ToString();
                        model.Usaged = DT.Rows[j][7].ToString();
                        model.HostName = DT.Rows[j][8].ToString();
                        model.GuaranteeClause = DT.Rows[j][9].ToString();
                        model.ExpireTime = Convert.ToDateTime(DT.Rows[j][10]);
                        model.BuyTime = Convert.ToDateTime(DT.Rows[j][11]);
                        model.DBAndVersion = DT.Rows[j][12].ToString();
                        model.MidAndVersion = DT.Rows[j][13].ToString();
                        model.SecAndVersion = DT.Rows[j][14].ToString();
                        model.OSAndVersion = DT.Rows[j][15].ToString();
                        model.Company = DT.Rows[j][16].ToString();

                        model.MarkAsDeleted = 0;
                        model.DataSetId = "BMC.ASSET";
                        model.Submitter = "Demo";
                        //model.Create_Date = DateTime.Now;
                        //model.Last_Modifed_By = "Demo";
                        //model.Modifed_Date = DateTime.Now;
                        model.SubmitFullName = "Demo";
                        model.ModifyFullName = "Demo";
                        model.IP = model.IPAddress;
                        
                        model.zSchemaProperName = "BMC_SysSoftware";
                        model.Short_Description = "BMC_SysSoftware";
                        model.zTmpKeyword = "BMC.CORE:BMC_SYSSOFTWARE";
                        model.ClassId = "BMC.CORE:BMC_SYSSOFTWARE";
                        model.UserDisplayObjectName = "BMC_SysSoftware";



                        bool ret = DataInit.ImportSysSoftware(model);
                        string msg = "Import " + DT.Rows[j][0].ToString() + " DeviceName = " + model.DeviceName + " ;" + ret.ToString();
                        if (ret)
                            SysLog.WriteSysLog(msg);
                        Console.WriteLine(msg);

                    }
                }

            }

            Console.WriteLine(" All CI data were imported!");
            Console.ReadLine();

        }
        
    }
}
